package com.briup.recognition.service.impl;

import com.briup.recognition.bean.GreenhouseMessage;
import com.briup.recognition.bean.GreenhouseMessageExample;
import com.briup.recognition.dao.GreenhouseMessageMapper;
import com.briup.recognition.service.IMessageService;
import com.briup.recognition.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements IMessageService {
    @Resource
    private GreenhouseMessageMapper greenhouseMessageMapper;

    @Override
    public List<GreenhouseMessage> query() {
        GreenhouseMessageExample example=new GreenhouseMessageExample();
        return greenhouseMessageMapper.selectByExample(example);
    }

    @Override
    public Integer saveOrUpdate(GreenhouseMessage message) {
        GreenhouseMessageExample example=new GreenhouseMessageExample();
        long cout=greenhouseMessageMapper.countByExample(example);
        message.setQuestionTime(new Date().getTime());
        message.setReplayTime(new Date().getTime());

        System.out.println(cout);
        System.out.println(message.getId());
        if (message.getId()==null){
            if(message.getStatus()==null){
                message.setStatus("未处理");
            }
            greenhouseMessageMapper.insertSelective(message);
            return 1;
        }else if (message.getId()!=null &&message.getId()<=cout+1){
            greenhouseMessageMapper.updateByPrimaryKey(message);
            return 2;
        }
        else return -1;
    }

    @Override
    public void deleteById(long id) {
            greenhouseMessageMapper.deleteByPrimaryKey(id);
    }

}
